home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / StateManager / snow.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  5.1 KB  |  148 lines

  1. //--------------------------------------------------------------------------------------
  2. //
  3. // Snow Lighting Model
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. //--------------------------------------------------------------------------------------
  7.  
  8.  
  9. //--------------------------------------------------------------------------------------
  10. // Scene Setup
  11. //--------------------------------------------------------------------------------------
  12.  
  13. // light direction (world space)
  14. float3 lightDir = {0.577, -0.577, 0.577};
  15.  
  16. // light intensity
  17. float4 I_a = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
  18. float4 I_d = { 0.6f, 0.6f, 0.6f, 1.0f };    // diffuse
  19. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  20.  
  21.  
  22. // Transformation Matrices
  23. matrix matWorld : WORLD;
  24. matrix matViewProj  : VIEWPROJECTION;
  25. matrix matViewInv   : VIEWINV;              // VIEWINV not supported by Effect Edit
  26.  
  27. texture Texture0 <  string type = "CUBE"; string name = "ripples.dds"; >;
  28.  
  29. //--------------------------------------------------------------------------------------
  30. // Material Properties
  31. //--------------------------------------------------------------------------------------
  32.  
  33. // Set by EffectInstance when mesh is loaded
  34. // (Default values provided for Effect Edit)
  35. float4 Diffuse = float4( 0.95f, 0.95f, 1.f, 1.f );
  36. float4 Ambient = float4( 0.95f, 0.95f, 1.f, 1.f );
  37. float4 Specular = float4( 0.2f, 0.2f,  0.2f, 1.f );
  38.  
  39.  
  40. //--------------------------------------------------------------------------------------
  41. // Vertex Shader
  42. //--------------------------------------------------------------------------------------
  43. void VS( in  float3 pos      : POSITION,
  44.          in  float3 norm     : NORMAL,
  45.          in  float2 iT0      : TEXCOORD0,
  46.          in  float3 Tangent  : TANGENT0,
  47.          out float4 oPos     : POSITION,
  48.          out float2 oT0      : TEXCOORD0,
  49.          out float3 oToLight : TEXCOORD1,
  50.          out float3 oHalf    : TEXCOORD2 )
  51. {
  52.     // Transform the vertex to clip space
  53.     float4 Pos_w = mul( float4(pos,1), matWorld );
  54.     oPos = mul( Pos_w, matViewProj );
  55.  
  56.     // Calculate the tangent vectors in world space
  57.     float3 Normal_w = mul( norm, (float3x3)matWorld );
  58.     float3 Tangent_w = mul( Tangent, (float3x3)matWorld );
  59.     float3 Binormal_w = cross( Tangent_w, Normal_w );
  60.  
  61.     // Calculate the tangent matrix
  62.     float3x3 matTSpace = transpose(float3x3( Tangent_w, Binormal_w, Normal_w  ));
  63.  
  64.     // The Eye Position in World space is the last row of the 
  65.     // full-inverse of the view transform
  66.     float3 EyePos_w = matViewInv[3];
  67.     float3 ToEye_w = normalize( EyePos_w - (float3)Pos_w );
  68.     
  69.     // Half Vector for the sparkly refections
  70.     // The sparkles are computed by masking per-pixel specular with a specular mask
  71.     // in the texture alpha channel.
  72.     float3 Half_w = normalize( ToEye_w - lightDir );
  73.  
  74.     // Take the light and half-vector into tangent space
  75.     float3 Half_t = mul( Half_w, matTSpace );
  76.     float3 Light_t = mul( -lightDir, matTSpace );
  77.  
  78.     // Pack -1,1 into 0,1
  79.     oHalf = 0.5f * Half_t + 0.5f;
  80.     oToLight = 0.5f * Light_t + 0.5f;
  81.  
  82.     // pass TexCoords through
  83.     oT0 = iT0;
  84. }
  85.  
  86.  
  87. sampler normal_sampler = sampler_state
  88. {
  89.     Texture = (Texture0);
  90.     MinFilter = LINEAR;
  91.     MagFilter = LINEAR;
  92.     MipFilter = LINEAR;
  93. };
  94.  
  95. //--------------------------------------------------------------------------------------
  96. // Pixel Shader
  97. //--------------------------------------------------------------------------------------
  98. float4 PS( in  float2 iT0      : TEXCOORD0,
  99.            in  float3 iToLight : TEXCOORD1,
  100.            in  float3 iHalf    : TEXCOORD2 ) : COLOR0
  101. {
  102.     // Read the normal direction from the texture
  103.     float4 NormalMap = tex2D( normal_sampler, iT0 );
  104.     
  105.     // Unpack the Normal, Light, and Half-Vector from 0,1 to -1,1
  106.     float3 Normal  = NormalMap * 2 - 1;
  107.     float3 ToLight = iToLight * 2 - 1;
  108.     float3 Half = iHalf * 2 - 1;
  109.  
  110.     // Calculate the diffuse coefficient
  111.     float NdotL = dot( Normal.rgb, ToLight);
  112.     
  113.     // Calcuate the specular coefficient
  114.     float NdotH = dot( Normal.rgb, Half );
  115.     NdotH *= NdotH;
  116.     NdotH *= NdotH;
  117.     NdotH *= NdotH;
  118.  
  119.     // Modulate the Diffuse and Specular colors by the material properties
  120.     float3 DiffuseC = Diffuse* NdotL * I_d + I_a * Ambient;
  121.     float3 SpecularC = (float3)Specular * NdotH * NormalMap.a;
  122.  
  123.     // Return the final color    
  124.     return float4( DiffuseC+SpecularC, 1.f);
  125. }
  126.  
  127.  
  128. //--------------------------------------------------------------------------------------
  129. // Default Technique
  130. // Establishes Vertex and Pixel Shader
  131. // Ensures base states are set to required values
  132. // (Other techniques within the scene perturb these states)
  133. //--------------------------------------------------------------------------------------
  134. technique tec0
  135. {
  136.     pass p0
  137.     {
  138.         VertexShader = compile vs_1_1 VS();
  139.         PixelShader  = compile ps_1_4 PS();
  140.  
  141.         ZEnable          = TRUE;
  142.         ZWriteEnable     = TRUE;
  143.         AlphaBlendEnable = FALSE;
  144.         CullMode         = CCW;
  145.         AlphaTestEnable  = FALSE;
  146.     }
  147. }
  148.